package com.example.web.service.impl;

import com.example.web.mapper.UserTMapper;
import com.example.web.pojo.Result;
import com.example.web.pojo.UserT;
import com.example.web.pojo.dto.NewPassword;
import com.example.web.service.UserTService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;


@Service
public class UserTServiceImpl implements UserTService {


    @Autowired
    private UserTMapper userTMapper;

    //登录
    @Override
    public Result login(UserT userT) {
        if (userT.getName() == null || userT.getPassword() == null){
            return Result.error("用户名或密码不能为空");
        }
        if (userTMapper.selectByNameAndPassword(userT.getName(), userT.getPassword()) != null) {
            return Result.success();
        }
        return Result.error("用户名或密码错误");
    }

    //注册
    @Override
    public Result register(UserT userT) {
        if (userT == null || userT.getName() == null || userT.getPassword() == null || userT.getEmail() == null) {
            return Result.error("用户名或密码或邮箱不能为空");
        }
        if (userTMapper.selectByName(userT.getName()) == null) {
            userT.setIsDelete((short) 0);
            userTMapper.insert(userT);
            return Result.success();
        }
        return Result.error("用户名已存在");
    }

    //修改密码
    @Override
    public Result updatePassword(NewPassword newPassword) {
        if (userTMapper.updatePassword(newPassword) == 1) {
            return Result.success();
        }
        return Result.error("密码错误");
    }

    //删除
    @Override
    public Result delete(String name) {
        if (userTMapper.deleteByName(name) == 1) {
            return Result.success();
        }
        return Result.error("用户不存在");
    }
}
